function! HackDesert()
    highlight Normal       guibg=#272822 guifg=#FFFFFF
    highlight LineNr       guibg=#272822 guifg=#9F9F9F
    highlight Cursor       guibg=#00EE00 guifg=#FF0000
    highlight MatchParen   guibg=grey30  guifg=grey60
    highlight Visual       guibg=#383838 guifg=#9F9F9F
    highlight Pmenu        guibg=grey30  guifg=grey99
    highlight PmenuSel     guibg=grey45  guifg=grey99
    highlight PmenuThumb   guibg=grey50  guifg=grey70
    highlight NonText      guibg=#272822 guifg=#272822 ctermfg=black
    highlight ModeMsg      guibg=#272822 guifg=#9F9F9F gui=NONE
    highlight ErrorMsg     guibg=#272822 guifg=#9F9F9F gui=NONE
    highlight WarningMsg   guibg=#272822 guifg=#9F9F9F gui=NONE
    highlight CursorLineNr guibg=grey40  guifg=#FFFFFF gui=NONE
    highlight Search       guibg=#FF6633 guifg=#FFFFFF gui=NONE
    highlight IncSearch    guibg=#FF6633 guifg=#FFFFFF gui=NONE
    highlight Over         guibg=grey30  guifg=grey40
    highlight SpecialKey   guibg=#272822 guifg=yellowgreen gui=NONE

    highlight Comment      guifg=grey40     gui=NONE
    highlight Constant     guifg=#FFA0A0    gui=NONE
    highlight Identifier   guifg=#5CD9EF    gui=NONE
    highlight Statement    guifg=#F92672    gui=NONE
    highlight PreProc      guifg=#F92672    gui=NONE
    highlight Type         guifg=#5CD9EF    gui=NONE
    highlight Special      guifg=#FFA0A0    gui=NONE
    highlight Function     guifg=#A6E22E    gui=NONE
    highlight Ignore       guifg=grey40     gui=NONE
    highlight Todo         guifg=orangered guibg=yellow2 gui=NONE

    highlight cppStatement guifg=#F92672 gui=NONE
    highlight Exception    guifg=#5CD9EF gui=NONE
    highlight Operator     guifg=#5CD9EF gui=NONE
    highlight StorageClass guifg=#5CD9EF gui=NONE
    highlight Structure    guifg=#5CD9EF gui=NONE
    highlight Boolean      guifg=#AE81FF gui=NONE

    highlight User1        guibg=#00AFD7 guifg=grey15
    highlight User2        guibg=grey30  guifg=grey70
    highlight User3        guibg=grey19  guifg=grey60
    highlight User4        guibg=grey40  guifg=grey80
    highlight User5        guibg=grey50  guifg=grey90
    highlight User6        guibg=grey19  guifg=#CC33CC
    highlight User7        guibg=#FF33CC guifg=grey15
    highlight User8        guibg=#FF6633 guifg=grey15
    highlight User9        guibg=#9933CC guifg=grey15
    highlight StatusLine   guibg=white   guifg=grey99
    highlight StatusLineNC guibg=white   guifg=grey98

    highlight TabLine      guibg=grey30  guifg=#FFFFFF  ctermbg=black ctermfg=white term=NONE cterm=NONE gui=NONE
    highlight TabLineFill  guibg=grey30  guifg=#FFFFFF  ctermbg=black ctermfg=white term=NONE cterm=NONE gui=NONE
    highlight TabLineSel   guibg=#272822 guifg=#FFFFFF  ctermbg=black ctermfg=white term=NONE cterm=NONE gui=NONE
endfunction

function! GuiSetting()
    if (has("gui_running"))
        set guioptions=
        set showtabline=2
        set clipboard+=unnamed
        set guitablabel=%t\ %M
        autocmd GUIEnter * set visualbell t_vb=
        autocmd BufRead,TabEnter * let &titlestring=expand("%:p")
    endif
endfunction

function! CursorPostion()
    if (!exists("s:init_CursorPostion"))
        let s:init_CursorPostion=1
        autocmd BufRead * call CursorPostion()
        return
    endif
    if (!(line("'\"") <= 1 || line("'\"") > line("$")))
        silent execute "normal! g`\""
        silent execute "normal! zz"
    endif
endfunction

function! ToggleMaxWindow()
    if (has("gui_running") && has("win32"))
        if (!exists("s:max_win"))
            let s:max_win=0
        endif
        silent execute s:max_win ? "simalt ~R" : "simalt ~X"
        let s:max_win=!s:max_win
    endif
endfunction

function! SyntaxCpp()
    syntax match cCustomParen "(" contains=cParen contains=cCppParen
    syntax match cCustomFunc  "\w\+\s*(\@=" contains=cCustomParen
    highlight def link cCustomFunc  Function
endfunction

function! Colorscheme()
    syntax on
    colorscheme desert
    filetype plugin indent on
    if (g:colors_name == "desert")
        call HackDesert()
    endif
    autocmd BufRead *.h,*.cpp call SyntaxCpp()
endfunction

function! Netrw()
    let g:netrw_liststyle=3
    let g:netrw_sort_sequence="[\/]$,*"
endfunction

function! ChineseGarbled()
    if (has("gui_running") && has("win32"))
        language messages en
        set langmenu=zh_CN.utf-8
        source $VIMRUNTIME/delmenu.vim
        source $VIMRUNTIME/menu.vim
    endif
endfunction

function! NopMiddleMouse()
    nnoremap <silent> <MiddleMouse>   <Nop>
    nnoremap <silent> <2-MiddleMouse> <Nop>
    nnoremap <silent> <3-MiddleMouse> <Nop>
    nnoremap <silent> <4-MiddleMouse> <Nop>
    inoremap <silent> <MiddleMouse>   <Nop>
    inoremap <silent> <2-MiddleMouse> <Nop>
    inoremap <silent> <3-MiddleMouse> <Nop>
    inoremap <silent> <4-MiddleMouse> <Nop>
endfunction

function! ResizeWindow(line, column)
    if (has("gui_running"))
        silent execute "set lines=".a:line
        silent execute "set columns=".a:column
    endif
endfunction

function! SetWindowPosition(x, y)
    if (has("gui_running"))
        silent execute "autocmd GUIEnter * winpos ".a:x." ".a:y
    endif
endfunction

function! GetDesktopSize()
    if (has("win32"))
        let file = eval('$Temp').'\desktop_size.vbs'
        if (!executable(file))
            let vbs =
                        \[
                        \'For Each Monitor in GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_DesktopMonitor",,48)',
                        \'    WScript.echo(Monitor.ScreenWidth & "x" & Monitor.ScreenHeight)',
                        \'Next',
                        \]
            call writefile(vbs, file)
        endif
        return matchstr(system("cscript /nologo ".file), ".\\{-}\\ze\n")
    endif
endfunction

function! AdjustPosAndSize()
    if (has("gui_running"))
        let desktop_size = GetDesktopSize()
        if (desktop_size=="800x600")
            call SetWindowPosition(250,20)
            call ResizeWindow(30,70)
        endif
        if (desktop_size=="1024x768")
            call SetWindowPosition(350,20)
            call ResizeWindow(40,90)
        endif
        if (desktop_size=="1280x800")
            call SetWindowPosition(520,20)
            call ResizeWindow(42,100)
        endif
        if (desktop_size=="1366x768")
            call SetWindowPosition(550,20)
            call ResizeWindow(42,113)
        endif
        if (desktop_size=="1440x900")
            call SetWindowPosition(600,20)
            call ResizeWindow(50,112)
        endif
        if (desktop_size=="1920x1080")
            call SetWindowPosition(650,70)
            call ResizeWindow(55,120)
        endif
    endif
endfunction

function! ShowOver(num, filetype)
    silent execute "autocmd CursorMoved ".a:filetype." match Over '\\%>".a:num."v[^$]'"
endfunction

function! ShowNumber()
    autocmd CursorMoved * set number
endfunction

function! ConvertToNoTailSpace(filetype)
    if (!exists("s:init_ConvertToNoTailSpace"))
        let s:init_ConvertToNoTailSpace=1
        silent execute "autocmd BufWrite ".a:filetype." call ConvertToNoTailSpace('')"
        return
    endif
    let pos=getpos(".")
    if(search("\\s\\+$") > 0)
        silent execute "%s/\\s\\+$//g"
    endif
    call setpos(".",pos)
endfunction

function! ConvertToUnix(filetype)
    if (!exists("s:init_ConvertToUnix"))
        let s:init_ConvertToUnix=1
        silent execute "autocmd BufWrite ".a:filetype." call ConvertToUnix('')"
        return
    endif
    let pos=getpos(".")
    set fileformat=unix
    if(search("\r$") > 0)
        silent execute "%s/\r$//g"
    endif
    call setpos(".",pos)
endfunction

function! ConvertToUtf8(filetype)
    if (!exists("s:init_ConvertToUtf8"))
        let s:init_ConvertToUtf8=1
        silent execute "autocmd BufWrite ".a:filetype." call ConvertToUtf8('')"
        return
    endif
    set fileencoding=utf-8
endfunction

function! ConvertToSpace(filetype)
    if (!exists("s:init_ConvertToSpace"))
        let s:init_ConvertToSpace=1
        silent execute "autocmd BufWrite ".a:filetype." call ConvertToSpace('')"
        return
    endif
    retab
endfunction

function! ConvertToBomb(filetype)
    if (!exists("s:init_ConvertToBomb"))
        let s:init_ConvertToBomb=1
        silent execute "autocmd BufWrite ".a:filetype." call ConvertToBomb('')"
        return
    endif
    set bomb
endfunction

function! RecordCursorPos()
    if (!exists("b:cursor_history"))
        let b:cursor_history=[]
        let b:cursor_index=0
        let b:cursor_record=1
    endif
    if (!b:cursor_record)
        let b:cursor_record=1
        return
    endif
    let pos=getpos(".")
    if (len(b:cursor_history) > 0 && pos == b:cursor_history[b:cursor_index])
        return
    endif
    call add(b:cursor_history, pos)
    if (len(b:cursor_history) > 1000)
        call remove(b:cursor_history, 0)
    endif
    let b:cursor_index=len(b:cursor_history)-1
endfunction

function! ForwardCursor()
    let b:cursor_record=0
    let b:cursor_index=b:cursor_index-1
    if b:cursor_index == -1
        let b:cursor_index=0
    endif
    let pos=b:cursor_history[b:cursor_index]
    call setpos(".", pos)
endfunction

function! BackCursor()
    let b:cursor_record=0
    let b:cursor_index=b:cursor_index+1
    if b:cursor_index == len(b:cursor_history)
        let b:cursor_index=len(b:cursor_history)-1
    endif
    let pos=b:cursor_history[b:cursor_index]
    call setpos(".", pos)
endfunction

function! CursorHistory()
    autocmd CursorMoved * call RecordCursorPos()
    nnoremap <silent> <Tab> <Esc>:call ForwardCursor()<CR>
    nnoremap <silent> <Space> <Esc>:call BackCursor()<CR>
endfunction

function! Mode()
    let mode = mode()
    let modes=
                \{
                \    'n' : '   Normal   ',
                \    'i' : '   Insert   ',
                \    'v' : '   Visual   ',
                \    'V' : 'Visual Line ',
                \    '': 'Visual Block',
                \    'R' : '   Replace  ',
                \}
    if (has_key(modes, mode))
        let mode = modes[mode]
    endif
    return mode
endfunction

function! Color()
    let mode = mode()
    let color = '1'
    let colors =
                \{
                \'n' : '1',
                \'i' : '7',
                \'v' : '8',
                \'V' : '8',
                \'': '8',
                \'R' : '9',
                \}
    if (has_key(colors, mode))
        let color = colors[mode]
    endif
    return color
endfunction

function! StatusLine()
    return
                \"%".Color()."*\ %-12.12{Mode()}\ ".
                \"%2*\ %<%t\ %6*%M".
                \"%3*\ %=\ ".
                \"%3*\ %{&fileformat}\ │".
                \"%3*\ %{(&fenc!=''?&fenc:&enc)}\ │".
                \"%3*\ %6.6{&bomb?' bomb ':'nobomb'}\ \ ".
                \"%4*\ %3.3p%%\ \ ".
                \"%5*\ %5.5l,%-5.5c\ "
endfunction

function! TabLabel(index)
    let buflist = tabpagebuflist(a:index)
    let winnr   = tabpagewinnr(a:index)
    let name    = fnamemodify(bufname(buflist[winnr-1]), ':t')
    if (name == "")
        let name = "[No Name]"
    endif
    return name
endfunction

function! TabLine()
    let line = ''
    for i in range(tabpagenr('$'))
        if i + 1 == tabpagenr()
            let line .= '%#TabLineSel#'
            let line .= ' %'.(i+1).'T%{TabLabel('.(i+1).')} '
        else
            let line .= '%#TabLine#'
            let line .= ' %'.(i+1).'T%{TabLabel('.(i+1).')} '
        endif
        if(!has("gui_running"))
            let line .= ' '
        endif
    endfor
    let line .= '%#TabLineFill#%T%=%#TabLine#%999X×'
    return line
endfunction

function! RemoteTabOpen()
    if (has("gui_running") && has("win32"))
        if argc() == 1
            let file = iconv(argv(0), 'utf-8', 'gbk')
            call system('gvim --servername gvim_server --remote-tab-silent "'.file.'"')
            call remote_foreground("gvim_server")
            silent execute "q"
        endif
    endif
endfunction

function! Select(list)
    let size=len(a:list)+1
    silent execute "set cmdheight=".size | redraw
    for i in range(len(a:list))
        echohl SpecialKey | echon i+1."."
        echohl Type       | echon (eval("&".a:list[i]) ? "":"no").a:list[i]
        echohl None       | echo ""
    endfor
    let index = getchar() - 49 | set cmdheight=1
    if(index < 0 || index >= size-1) | return "" | endif
    return a:list[index]
endfunction

function! ToggleOptions()
    let options=["cursorline","cursorcolumn","wrap","paste","list","expandtab","bomb","lazyredraw"]
    let option=Select(options)
    if(option=="") | return | endif
    silent execute "set ".option."!"
    execute "set ".option."?"
endfunction
